﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using VCN.App.DataAccessLayer;
using VCN.App.DataTransferObjects;

namespace VCN.admin
{
    public partial class AddProductRelative : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack == false)
            {
                this.LoadDataToDdl();
                this.LoadDataToDdlProduct();
                Page.Title = "Thêm sản phẩm liên quan";
            }
        }
        private void LoadDataToDdl()
        {
            CategoriesDAO v_CategoryDAO = new CategoriesDAO();
            DataTable v_dt = v_CategoryDAO.selectAll2Dt();
            ddl_Category.DataSource = v_dt;
            ddl_Category.DataTextField = "CATEGORY_NAME";
            ddl_Category.DataValueField = "Id";
            ddl_Category.DataBind();
        }
        private void LoadDataToDdlProduct()
        {
            ProductDAO v_ProductDAO = new ProductDAO();
            DataTable v_dt = v_ProductDAO.SelectByCategoryId(int.Parse(ddl_Category.SelectedValue));
            ddl_Product.DataSource = v_dt;
            ddl_Product.DataTextField = "PRODUCT_NAME";
            ddl_Product.DataValueField = "Id";
            ddl_Product.DataBind();
        }


        protected void btnAdd_click(object sender, EventArgs e)
        {   if (FileUpload.HasFile)
            {
                RelativeProduct v_RelativeProduct = new RelativeProduct();
                v_RelativeProduct.ProductId = int.Parse(ddl_Product.SelectedItem.Value);
                v_RelativeProduct.Image = this.doUploadPImage();
                v_RelativeProduct.Name = m_txt_RelativeProductName.Text;
                RelativeProductDAO v_RelativeProductDAO = new RelativeProductDAO();
                v_RelativeProductDAO.insert(v_RelativeProduct);
                Response.Redirect("RelativeList.aspx");
            }
            else lbl_Error.Visible = true;
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {

        }

        protected void btnCancel_click(object sender, EventArgs e)
        {
            Response.Redirect("RelativeList.aspx");
        }
        private string doUploadPImage()
        {
            if (FileUpload.HasFile)
            {
                var v_str_fstorage = string.Format("../Images/Product/{0}", FileUpload.FileName);
                this.FileUpload.SaveAs(Server.MapPath(v_str_fstorage));
            }
            return FileUpload.FileName;
        }

        protected void ddl_Category_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.LoadDataToDdlProduct();
        }


    }
}
